package com.java.control.search;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.java.common.DateUtil;

/**
 * 订单
 * @author wqf
 * @version  Nov 24, 2011 11:55:45 AM
 */
public class OrderSearchCond extends SearchCond{
	
	private String orderCode;
	private String country;
	private String customer;
	private Long userId;
	private String company;
	private String userName;
	private Long status;
	private String year; 
	
	/**
	 * 下单开始时间
	 */
	private Date beginDate;
	
	/**
	 * 下单结束时间
	 */
	private Date endDate;
	
	public Long getUserId() {
		return userId;
	}


	public void setUserId(Long userId) {
		this.userId = userId;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getCustomer() {
		return customer;
	}


	public void setCustomer(String customer) {
		this.customer = customer;
	}


	public ConditionPart getCondition(){
		ConditionPart cp=new ConditionPart();
		StringBuffer searchStr=new StringBuffer(" 1=1");
		if(StringUtils.isNotBlank(this.searchWord)){
			searchStr.append(" and (user.userName like ?");
			cp.getParams().add("%"+this.searchWord+"%");
			searchStr.append(" or orderCode like ?)");
			cp.getParams().add("%"+searchWord+"%");
		}
		if(null!=userId){
			searchStr.append(" and user.id=?");
			cp.getParams().add(userId);
		}
		if(StringUtils.isNotBlank(country)){
			searchStr.append(" and country=?");
			cp.getParams().add(country);
		}
		if(StringUtils.isNotBlank(customer)){
			searchStr.append(" and customer=?");
			cp.getParams().add(customer);
		}
		if(null!=orderCode){
			searchStr.append(" and orderCode like ?");
			cp.getParams().add(orderCode+"%");
		}
		
		if (this.beginDate != null){
			searchStr.append(" and orderDate>=?");
			cp.getParams().add(this.beginDate);
		}
		if (this.endDate != null){
			searchStr.append(" and orderDate<=?)");
			cp.getParams().add(this.endDate);
		}
		if(StringUtils.isNotBlank(company)){
			searchStr.append(" and customerCompany like ?");
			cp.getParams().add(this.company);
		}
		if(StringUtils.isNotBlank(userName)){
			searchStr.append(" and user.userName like ?");
			cp.getParams().add(this.userName);
		}
		if(null!=this.status){
			searchStr.append(" and status.id=?");
			cp.getParams().add(this.status);
		}
		if(StringUtils.isNotBlank(this.year)){			
			try {
				Date start = DateUtil.getDateTimefromString("yyyy", this.year);
				Date end = DateUtil.getDateAfterMonths(start, 12);				
				searchStr.append(" and closingDate between ? and ?");
				cp.getParams().add(start);
				cp.getParams().add(end);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		cp.setExpression(searchStr.toString());
		return cp;
	}


	public String getOrderCode() {
		return orderCode;
	}


	public void setOrderCode(String orderCode) {
		this.orderCode = orderCode;
	}


	public Date getBeginDate() {
		return beginDate;
	}


	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public String getCompany() {
		return company;
	}


	public void setCompany(String company) {
		this.company = company;
	}


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public Long getStatus() {
		return status;
	}


	public void setStatus(Long status) {
		this.status = status;
	}


	public String getYear() {
		return year;
	}


	public void setYear(String year) {
		this.year = year;
	}


}
